# coding=utf-8

import unittest
from lib import TestReport, sendmail, config_reader
import time
import os


def run(pattern,Subject):
    test_dir = r'/openmind-hub/auto_test/autotest/test_openmind_hub'
    suite = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern=pattern)
    now = time.strftime('%Y-%m-%d_%H_%M_%S')
    reportname = r'/openmind-hub/auto_test/autotest/report/testreport/' + 'TestResult' + now + '.html'
    with open(reportname, 'wb') as f:
        runner = TestReport.HTMLTestReport(
            stream=f,
            title='测试报告',
            description='Test the import testcase',
            images=True
        )
        print("用例开始运行")
        runner.run(suite)
        print("用例运行全部结束")
    time.sleep(3)
    # 发送邮件
    mail = sendmail.SendMail()
    mail.send(config,Subject)


if __name__ == '__main__':
    print("读取config文件内容")
    global config
    config = config_reader.read_config()

    os.environ["HUB_WHITE_LIST_PATHS"] = "/openmind-hub"
    os.environ["OPENMIND_HUB_ENDPOINT"] = "https://api.gitcode.com"
    run("test_thirdparty_gitcode.py", "gitcode社区工具链测试报告")



